////////////////////////////////////////////////////登陆注册修改密码////////////////////////////////////
const config = require('../config');
const md5 = require('md5');
const jsonWebToken = require('jsonwebtoken');

module.exports = function (app, query, jwt) {
  app.get('/user/test', jwt({ secret: config.secret }), function (req, res, next) {
    res.send('用户服务器已开启');
  });

  //登录
  app.post('/login', function (req, res, next) {
    let { username, password } = req.body.user;
    if (username && password) {
      password = md5(password);
      console.log("loginPassword", password)
      query('SELECT * FROM  user WHERE username=? and password=?', [username, password], function (err, rows, fields) {
        if (err) {
          const json = {
            code: false,
            message: err.message
          };
          console.log(json);
          res.json(json);
        } else {
          if (rows && rows.length > 0) {
            const profile = {
              userId: rows[0].userid,
              username: rows[0].username,
              authority:rows[0].authority,
            };
            const token = jsonWebToken.sign(profile, config.secret, { expiresIn: 60 * 60 });
            const json = {
              code: true,
              message: "用户登录成功",
              data: {
                token: token,
                userId: rows[0].userid,
                username: rows[0].username,
                authority:rows[0].authority,
              }
            };
            console.log(json);
            res.json(json);
          } else {
            const json = {
              code: false,
              message: "当前用户不存在"
            };
            console.log(json);
            res.json(json);
          }
        }
      });
    } else {
      const json = {
        code: false,
        message: "提交的帐号密码不完整"
      };
      console.log(json);
      res.json(json);
    }
  });


  //验证登录
  app.post('/checklogin', jwt({ secret: config.secret }), function (req, res, next) {
    const { userId } = req.user;
    query('SELECT * FROM  user WHERE userid=?', [userId], function (err, rows, fields) {
      if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else {
        if (rows && rows.length > 0) {
          const json = {
            code: true,
            message: "验证登录成功",
            data: {
              userId: rows[0].userid,
              username: rows[0].username,
              authority:rows[0].authority,
            }
          };
          console.log(json);
          res.json(json);
        }
        else {
          const json = {
            code: false,
            message: "无该用户存在"
          };
          console.log(json);
          res.json(json);
        }
      }
    });
  });

  //注册
  app.post('/register', function (req, res, next) {
    console.log('注册成功');
    const { username, email, password } = req.body;
    query('SELECT * FROM user WHERE username=?', [username], function (err, rows, field) {
      if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else {
        if (rows && rows.length > 0) {
          const json = {
            code: false,
            message: "用户名已存在，注册失败"
          };
          console.log(json);
          res.json(json);
        } else {
          const user = {
            username,
            email,
            password: md5(password)
          };
          query('insert into user set ?', user, function (error, result) {
            if (error) {
              const json = {
                code: false,
                message: error.message
              };
              console.log(json);
              res.json(json);
            } else {
              const json = {
                code: true,
                message: "注册成功"
              };
              console.log(json);
              res.json(json);
            }
          });
        }
      }
    });
  });

  //更改密码
  app.put('/passport', jwt({ secret: config.secret }), function (req, res, next) {
	  let { password, newpassword} = req.body; 
      password = md5(password); 
        console.log(password);
    const { userId } = req.user;
	    query('SELECT * FROM  user WHERE userid=? and password=?', [userId, password], function (err, rows, fields) {
        if (err) {
          const json = {
            code: false,
            message: err.message
          };
          console.log(json);
          res.json(json);
        } else {
          if (rows && rows.length > 0) {
			  newpassword=md5(newpassword);
			    query('update user set password=? WHERE userid=?',[newpassword, userId], function (err, rows, fields) {
      if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else {
            const json = {
          code: true,
          message: "修改成功"
        };
        console.log(json);
        res.json(json);
      }   }) 
        }
		}}); 
  
    });  	 	
  
  
  //获取个人信息
  app.get('/personalinformation', jwt({ secret: config.secret }), function (req, res, next) {  
    const { userId } = req.user;
   let sql='SELECT * FROM  user WHERE userid=? '
    query(sql, [userId], function (err, rows, fields) {
      if (err) {
            const json = {
            code: false,
            message: err.message
          };
          console.log(json);
          res.json(json);
      } else {
        if (rows && rows.length > 0) {
              const data = {
            id: rows[0].userid,   //id
            username: rows[0].username,    //用户名
            email: rows[0].email,    //用户邮箱
            password: rows[0].password,   //用户密码
            authority: rows[0].authority, //用户身份
          }
          const json = {
            code: true,
            message: "个人信息获取成功！",
            data
          };
          console.log(json);
          res.json(json);
        }
      }
    })
  });
  
  
  
  //修改个人信息
  app.put('/personalinformation', jwt({ secret: config.secret }), function (req, res, next) {  
    let { username, email} = req.body;    
	const params = [
       username,
       email, 
      req.user.userId
    ];  
    query('update user set username=?,email=? WHERE userid=?', params, function (err, rows, fields) {
      if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else {
            const json = {
          code: true,
          message: "修改成功"
        };
        console.log(json);
        res.json(json);
      }
    });
  });
};












